#!/usr/local/bin/python2.5
print "Please wait..."
verbose = True

if verbose: print "Importing Modules..."
import sys
import pottononline.feedparser
import  urllib
from pottononline.map.models import *
from pottononline.BeautifulSoup import BeautifulSoup

user = User.objects.get(pk=1) # These will then belong to the superuser

# First take the RSS feed and process it....
URL = "http://www.geograph.org.uk/feed/results/4928081.rss"
rssfeed = pottononline.feedparser.parse(URL)
# ...and then use the HTML to find the thumbnail.

URL = "http://www.geograph.org.uk/search.php?i=4928081"
fp = urllib.urlopen(URL)
searchfile = fp.read()
fp.close()
if verbose: print "Processing Soup..."
soup = BeautifulSoup(''.join(searchfile))

def find_thumbnail(photoid):
  for each in soup.findAll("a"):
    if each['href'].find(photoid) != -1:
      return each.find("img")["src"]
  return "Thumbnail not found"

def strip_html(htmlstring):
  return re.sub(r'<[^>]*?>', '', htmlstring) 

try:
  geoPhotoType = MarkerType.objects.get(name="Geophoto")
except:
  print "Error:  You probably have a new database, and haven't created the Marker Type of 'Geophoto'."
  sys.exit(-1)

# delete all existing geoPhotos ?
clean_up = raw_input("Delete *ALL* old GeoPhotos?  (y/n) [No]: ")
if clean_up == "y":
  for h in Marker.objects.filter(itemtype=geoPhotoType):
    h.delete()

for geopic in rssfeed['entries']:
  if verbose:  print geopic
  creator = geopic.author
  try:      link = geopic.link
  except:   link = None
  try:   title = geopic.title  
  except:  title = None
  
  descr = u''+geopic.description.replace(u'\x96','-')
  descr = descr.replace('<br />','  ')
  descr = strip_html(descr).strip()

  
  try:   #  HA!  Bad luck geograph.  Try to randomly change your RSS eh?
    location = geopic.georss_point
  except:  #  Eat Python!
    location = geopic.point
  
  lat = location.split(' ')[0]
  lon = location.split(' ')[1]
  updated = geopic.updated
  thumblink = find_thumbnail(geopic.link.split('/photo/')[1])
  if verbose:
    print creator
    print link
    print title
    print lon
    print lat
    print descr
    print updated
    print thumblink
    print "---------------------------"
  geograph = Marker()
  geograph.owner_user = user
  geograph.creator = creator
  geograph.lat = lat
  geograph.lon = lon
  geograph.itemtype = geoPhotoType
  geograph.link = link
  geograph.thumbnail = thumblink
  geograph.description = descr 
  geograph.name = title
  geograph.commentable = True
  geograph.rateable = True
  # geograph.updated = updated #  Actually, we don't care when it was updated!
  geograph.save() 
  
